package com.xing.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.xing.model.PaymentDetail;
import com.xing.util.Util;

@Repository
public class PaymentDetailDao {

	@Resource
	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
	public void addPaymentDetail(PaymentDetail info) {
		
		baseDao.getHibernateTemplate().save(info);
	}
	
	public void updatePaymentDetail(PaymentDetail info) {
		
		baseDao.getHibernateTemplate().saveOrUpdate(info);
	}
	
	public void deletePaymentDetail(List<Integer> idList) {
		
		String hql = "delete from PaymentDetail where intId in (:intId)";
		
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("intId", idList));
		
		baseDao.updateByHql(hql, paramModelList);
	}
	
	public Map<String, Object> getListAndCount(List<String> conditionList, Integer page, Integer pageRow) {
		StringBuilder fetchHql = new StringBuilder();
		fetchHql.append("select new map(pd.intId as intId,");
		fetchHql.append("c.contractCode as contractCode,");
		fetchHql.append("c.contractName as contractName,");
		fetchHql.append("c.contractId as contractId,");
		fetchHql.append("pd.contractPaymentId as contractPaymentId,");
		fetchHql.append("dp.paymentName as paymentName,");
		fetchHql.append("pd.paymentAmount as paymentAmount,");
		fetchHql.append("pd.invoice as invoice,");
		fetchHql.append("pd.invoiceAmount as invoiceAmount," );
		fetchHql.append("pd.paymentTime as paymentTime," );
		fetchHql.append("pd.requestTime as requestTime," );
		fetchHql.append("pd.handleUserId as handleUserId,");
		fetchHql.append("e.employeeName as handleUserName,");
		fetchHql.append("pd.remark as remark");
		fetchHql.append(")");
		
		StringBuilder hql = new StringBuilder();
		hql.append(" from PaymentDetail pd, ContractPayment cp, ContractInfo c, DicPayment dp, EmployeeInfo e ");
		hql.append(" where pd.contractPaymentId = cp.intId");
		hql.append("   and cp.contractId = c.contractId");
		hql.append("   and cp.paymentId = dp.paymentId");
		hql.append("   and pd.handleUserId = e.employeeId");
		
		if (!Util.isEmpty(conditionList)) {
			Iterator<String> ite = conditionList.iterator();
			while (ite.hasNext()) {
				hql.append(" and ");
				hql.append(ite.next());
			}
		}

		StringBuilder orderHql = new StringBuilder();
		orderHql.append(" order by pd.intId");
		
		Map<String, Object> result = baseDao.getListAndCount(fetchHql.toString(), hql.toString(), orderHql.toString(), page, pageRow);

		return result;
	}
	
	public List<Map<String, Object>> getContractPaymentList(String contractId) {
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new map(");
		    hql.append("cp.intId as intId,");
		    hql.append("cp.paymentRemark as paymentRemark,");
			hql.append("dp.paymentName as paymentName");
			hql.append(") from ContractPayment cp, DicPayment dp ");
			hql.append(" where cp.paymentId = dp.paymentId");
			hql.append("   and cp.contractId = ");
			hql.append(contractId);
			hql.append(" order by cp.paymentOrder");
			
		return (List<Map<String, Object>>) baseDao.getList(hql.toString());
	}
}
